package main

import "fmt"

func main() {
	// desserts := [2]string{"Cupcake", "Eclair"}
	// slice := desserts[0:1]
	// fmt.Println("Len of slice:", len(slice), "Cap of slice:", cap(slice), "Slice:", slice)

	desserts := [3]string{"Cupcake", "Eclair", "Ice cream"}
	slice := desserts[0:1] //从0开始分片的容量cap会和数组的容量一致
	fmt.Println("Array:", desserts)
	// slice := desserts[1:2] //分片的容量，是数组的容量减去分片的起始位置
	fmt.Println("Len of slice:", len(slice), "Cap of slice:", cap(slice), "Slice:", slice)

	fmt.Println()
	slice = append(slice, "Macaron")
	fmt.Println("Array:", desserts)
	fmt.Println("Len of slice:", len(slice), "Cap of slice:", cap(slice), "Slice:", slice)

	fmt.Println()
	slice = append(slice, "Cake")
	fmt.Println("Array:", desserts)
	fmt.Println("Len of slice:", len(slice), "Cap of slice:", cap(slice), "Slice:", slice)

	fmt.Println()

	slice = append(slice, "Juice") // 分片的容量已经满了，所以会重新分配一个新的数组，将原数组的值复制到新数组，再将新数组的地址赋值给分片，分片的容量会变为新数组的容量
	fmt.Println("Array:", desserts)
	fmt.Println("Len of slice:", len(slice), "Cap of slice:", cap(slice), "Slice:", slice)

	fmt.Println()
	slice[0] = "Chocolate" //上面分片已经超出了数组，所以再修改slice[0]不会修改数组的值
	fmt.Println("Array:", desserts)
	fmt.Println("Len of slice:", len(slice), "Cap of slice:", cap(slice), "Slice:", slice)
}
